﻿using MikuMikuLibrary.Databases;
using MikuMikuLibrary.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DatabaseConverter
{
    class TexMerge
    {
        public TextureDatabase db = new TextureDatabase();
        public TextureDatabase db2 = new TextureDatabase();
        public List<int> ids = new List<int>();
        public List<int> ids2 = new List<int>();
        public string logs = "";
        public void MergeAet()
        {
            int MaxId = 0;
            foreach (var i in db.Textures)
            {
                if (i.Id > MaxId)
                {
                    MaxId = i.Id;
                }
            }
            foreach (var i in db2.Textures)
            {
                var stringcheck = db.Textures.Where(c => c.Name == i.Name).FirstOrDefault();
                if (stringcheck == null)
                {
                   
                    var result = db.Textures.Where(c => c.Id == i.Id).FirstOrDefault();
                    if (result == null)
                    {
                        db.Textures.Add(i);
                    }
                    else
                    {
                        result = db.Textures.Where(c => c.Id == (ushort)(MaxId + 1)).FirstOrDefault();
                        while (result != null)
                        {
                            MaxId++;
                            result = db.Textures.Where(c => c.Id == (ushort)(MaxId + 1)).FirstOrDefault();
                        }
                        logs = logs + ("Warning: ID Changed from " + i.Id + " to " + (MaxId + 1)) + "\n";
                        i.Id = (ushort)(MaxId + 1);
                        db.Textures.Add(i);
                    }
                }
                else
                {
                    logs = logs + ("Warning : Skipped File " + i.Name) + "\n";
                }
            }
        }
    }
}
